package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.Menu;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.mapper.MenuMapper;
import com.zhitong.money.mapper.RoleMenuMapper;
import com.zhitong.money.services.api.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MenuService implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public List<Menu> findAllMenu() throws Exception {
        List<Menu> allMenu = menuMapper.findAllMenu();
        return allMenu;
    }

    @Override
    public Menu findMenuById(String id) throws Exception {
        Menu menuById = menuMapper.findMenuById(id);
        if (menuById==null){
            throw new NullPointerException("findMenuById:查询为空");
        }
        return menuById;
    }

    @Override
    public PageInfo findMenuByPage(Map<String,Object> map) throws Exception {

        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;
        PageHelper.startPage(pageNum,pageSize);
        List<Menu> menuByMap = menuMapper.findMenuByMap(map);
        PageInfo pageInfo=new PageInfo(menuByMap);
        return pageInfo;

    }

    @Override
    public List<Menu> findMenuByMap(Map<String, Object> map) throws Exception {
        return menuMapper.findMenuByMap(map);
    }

    @Override
    public void addMenu(Menu menu) throws Exception {
        Menu menuByName = menuMapper.findMenuByName(menu.getName());
        if (menuByName!=null){
            throw new RepeatException("信息重复!");
        }
        try {
            menuMapper.addMenu(menu);
        } catch (Exception e) {
            throw new AddException("addMenu:添加失败!"+e.getMessage());
        }
    }

    @Override
    public void updateMenu(Menu menu) throws Exception {
        Menu menuByName = menuMapper.findMenuByName(menu.getName());
        if (menuByName!=null && !menuByName.getId().equals(menu.getId())){
            throw new RepeatException("该菜单已经存在!");
        }
        try {
            menuMapper.updateMenu(menu);
        } catch (Exception e) {
            log.error("跟新菜单失败:"+e.getMessage());
            throw e;
        }
    }

    @Transactional
    @Override
    public int delMenu(String id) throws Exception {
        try {
            //check
            int i = roleMenuMapper.countByMid(id);
            if (i==0){
                setSubMenu(id);
                int i1 = menuMapper.delMenu(id);
                return i1;
            }
            return -1;
        } catch (Exception e) {
            log.error("删除菜单失败:"+e.getMessage());
            throw e;
        }
    }

    @Transactional
    @Override
    public int delMenuByIds(List<String> ids) throws Exception {
        try {
            int i = roleMenuMapper.countByMids(ids);
            if (i==0){
                for (String id : ids) {
                    setSubMenu(id);
                }
                int i1 = menuMapper.delMenus(ids);
                return i1;
            }
            return -1;
        } catch (Exception e) {
            throw new DeleteException("delMenuByIds:删除失败!");
        }
    }

    //删除一级菜单时,删除旗下的子菜单
    private void setSubMenu(String id){
        Menu menuById = menuMapper.findMenuById(id);
        if (menuById!=null){
            //如果删除的是父级菜单,则设置该子菜单的父Id为空
            if (menuById.getLevel().equals("01")){
                List<Menu> menuByParentId = menuMapper.findMenuByParentId(menuById.getId());
                //不为空时
                if (!menuByParentId.isEmpty()){
                    for (Menu menu : menuByParentId) {
                        menuMapper.delMenu(menu.getId());
                    }
                }
            }
        }
    }
}
